package hu.mapro.magicroller.core;

import hu.mapro.gamelib.Player;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.joints.WheelJointDef;

class MagicRollerPlayerBase {
	final MagicRollerConstants c;

	public MagicRollerPlayerBase(MagicRollerConstants constants) {
		super();
		this.c = constants;
	}
}
public class MagicRollerPlayer extends MagicRollerPlayerBase implements Player {

	// Car
	
	public Body carBody;
	private Body wheel1Body;
	private Body wheel2Body;

	
	public MagicRollerPlayer(MagicRollerConstants constants, World world) {
		super(constants);
		
		// - car
		
		PolygonShape platformShape = new PolygonShape();
		platformShape.setAsBox(constants.CAR_PLATFORM_WIDTH/2, constants.CAR_PLATFORM_HEIGHT/2, new Vector2(0, constants.CAR_WHEEL_RADIUS), 0);
//		Vector2 carPlatformVertices[] = new Vector2[] {
//				new Vector2(-CAR_PLATFORM_WIDTH/2.0f, CAR_PLATFORM_BOTTOM),
//				new Vector2(CAR_PLATFORM_WIDTH/2.0f, CAR_PLATFORM_BOTTOM),
//				new Vector2(CAR_PLATFORM_WIDTH/2.0f, CAR_PLATFORM_TOP),
//				new Vector2(-CAR_PLATFORM_WIDTH/2.0f, CAR_PLATFORM_TOP),
//		};
//		platformShape.set(carPlatformVertices);
		
		CircleShape wheelShape = new CircleShape();
		wheelShape.setRadius(constants.CAR_WHEEL_RADIUS);
		
		BodyDef carBodyDef = new BodyDef();
		carBodyDef.type = BodyType.DynamicBody;
		carBodyDef.position.set(constants.CAR_POSITION);
		
		carBody = world.createBody(carBodyDef);
		
		FixtureDef platformFixtureDef = new FixtureDef();
		platformFixtureDef.density = c.CAR_PLATFORM_DENSITY;
		
		
		platformFixtureDef.shape = platformShape;
		carBody.createFixture(platformFixtureDef);
		
		FixtureDef wheelFixtureDef = new FixtureDef();
		wheelFixtureDef.shape = wheelShape;
		wheelFixtureDef.density = c.WHEEL_DENSITY;
		wheelFixtureDef.friction = c.WHEEL_FRICTION;

		carBodyDef.position.set(constants.CAR_POSITION.x-constants.CAR_WHEEL_DISTANCE/2.0f, constants.CAR_POSITION.y + constants.CAR_WHEEL_RADIUS);
		wheel1Body = world.createBody(carBodyDef);
		wheel1Body.createFixture(wheelFixtureDef);

		carBodyDef.position.set(constants.CAR_POSITION.x+constants.CAR_WHEEL_DISTANCE/2.0f, constants.CAR_POSITION.y + constants.CAR_WHEEL_RADIUS);
		wheel2Body = world.createBody(carBodyDef);
		wheel2Body.createFixture(wheelFixtureDef);

		WheelJointDef jd = new WheelJointDef();
		Vector2 axis = new Vector2(0.0f, 1.0f);

		jd.initialize(carBody, wheel1Body, wheel1Body.getPosition(), axis);
		jd.motorSpeed = constants.MOTOR_SPEED;
		jd.maxMotorTorque = constants.MOTOR_TORQUE;
		jd.enableMotor = true;
		jd.frequencyHz = constants.SUSPENSION_FREQUENCY;
		jd.dampingRatio = constants.SUSPENSION_DAMPING;
		try {
			world.createJoint(jd);
		} catch (RuntimeException e) {}

		jd.initialize(carBody, wheel2Body, wheel2Body.getPosition(), axis);
		jd.motorSpeed = constants.MOTOR_SPEED;
		jd.maxMotorTorque = constants.MOTOR_TORQUE;
		jd.enableMotor = true;
		jd.frequencyHz = constants.SUSPENSION_FREQUENCY;
		jd.dampingRatio = constants.SUSPENSION_DAMPING;
		try {
			world.createJoint(jd);
		} catch (RuntimeException e) {}
		
		platformShape.dispose();
		wheelShape.dispose();
		
	}

	@Override
	public Vector2 getPosition() {
		return carBody.getWorldPoint(new Vector2(0, c.CAR_WHEEL_RADIUS));
	}
	
	void dispose() {
	}
	
}
